package com.fn.flutter_fn_sdk.page;

import android.app.Activity;
import android.util.Log;

import androidx.annotation.NonNull;

import com.fn.flutter_fn_sdk.PluginDelegate;
import com.fn.flutter_fn_sdk.event.AdEventAction;
import com.fn.sdk.api.fullScreenVideo.FnFullScreenAdListener;
import com.fn.sdk.api.fullScreenVideo.FnFullScreenVideoAd;

import io.flutter.plugin.common.MethodCall;

/**
 * 激励视频页面
 */
public class ScreenVideoPage extends BaseAdPage {
    private static final String TAG = ScreenVideoPage.class.getSimpleName();
    private Activity activity;
    private static ScreenVideoPage instance = null;

    public static ScreenVideoPage getInstance() {
        if (instance == null) {
            instance = new ScreenVideoPage();
        }
        return instance;
    }
    @Override
    public void loadAd(Activity activity, @NonNull MethodCall call) {
        this.activity = activity;
        // 获取参数
        posId = call.argument(PluginDelegate.KEY_POSID);
        loadAd();
    }

    private void loadAd(){
        FnFullScreenVideoAd fnFullScreenVideoAd = new FnFullScreenVideoAd();
        fnFullScreenVideoAd.setAdDirection(FnFullScreenVideoAd.VERTICAL);
        fnFullScreenVideoAd.loadFullAd(activity, posId, new FnFullScreenAdListener() {
            @Override
            public void onError(int code, String msg, String detail) {
                String message = String.format("error [%d - %s]", code, msg, detail);
                Log.e(TAG, "onError adError:" + code+"msg:"+msg+"detail:"+detail);
                sendErrorEvent(code, message);
            }

            @Override
            public void onLoad() {
                Log.i(TAG, "onLoad");
                sendEvent(AdEventAction.onAdLoaded);
            }

            @Override
            public void onSkipped() {
                Log.i(TAG, "onSkipped");
                sendEvent(AdEventAction.onAdSkip);
            }

            @Override
            public void onClick() {
                Log.i(TAG, "onClick");
                sendEvent(AdEventAction.onAdClicked);
            }

            @Override
            public void onClose() {
                Log.i(TAG, "onClose");
                sendEvent(AdEventAction.onAdClosed);
            }

            @Override
            public void onPlayStart() {
                Log.i(TAG, "onPlayStart");
                sendEvent(AdEventAction.onAdExposure);
            }

            @Override
            public void onPlayEnd() {
                Log.i(TAG, "onPlayEnd");
                sendEvent(AdEventAction.onAdComplete);
            }
        });
    }

    public void closeAd(){
        Log.e(TAG, "closeScreenVideoAd  closeAd");

        activity.finish();
    }

}
